package top.szhome.system.service.impl;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.szhome.framework.core.tools.util.ObjectUtil;
import top.szhome.framework.core.tools.util.TreeUtil;
import top.szhome.framework.starter.db.support.SimpleServiceImpl;
import top.szhome.system.convert.MenuConverter;
import top.szhome.system.dto.MenuDTO;
import top.szhome.system.entity.Menu;
import top.szhome.system.mapper.MenuMapper;
import top.szhome.system.service.MenuService;
import top.szhome.system.service.RoleMenuService;
import top.szhome.system.vo.MenuVO;

import java.awt.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * 菜单 服务实现
 *
 * @author 唐加利
 * @date 2023/07/31
 **/
@AllArgsConstructor
@Service
@Slf4j
public class MenuServiceImpl extends SimpleServiceImpl<MenuMapper, MenuConverter, MenuDTO, Menu, MenuVO> implements MenuService {

    private final RoleMenuService roleMenuService;

    @Override
    public List<MenuVO> getMenusOfRole(Long roleId, boolean tree) {
        List<Long> menuIds = roleMenuService.getMenusOfRole(roleId);
        if(ObjectUtil.isEmpty(menuIds)){
            return new ArrayList<>(0);
        }

        // 确保顺序
        List<Menu> menus = listByIds(menuIds);
        menus.sort(Comparator.comparing(Menu::getSort));
        List<MenuVO> menuVOS = getBaseConvert().to(menus);
        return tree ? TreeUtil.tree(menuVOS) : menuVOS;
    }

}
